import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class Main extends JFrame implements ActionListener {
    private JTextField inputField;
    private JButton saveButton, button1, button2;

    public Main() {
        inputField = new JTextField(20);
        inputField.setEditable(false);
        add(inputField, BorderLayout.NORTH);

        JPanel panel = new JPanel();
        button1 = new JButton("1");
        button2 = new JButton("2");
        saveButton = new JButton("保存");

        // TODO：为按钮添加监听器
        // ___
        button1.addActionListener(this);
        button2.addActionListener(this);
        saveButton.addActionListener(this);

        panel.add(button1);
        panel.add(button2);
        panel.add(saveButton);
        add(panel, BorderLayout.CENTER);

        setSize(300, 150);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO：填写事件处理逻辑
        String cmd = e.getActionCommand();
        // ____
        if("1".equals(cmd)||"2".equals(cmd)){
            inputField.setText(inputField.getText()+cmd);
        }else if("保存".equals(cmd)){
            try(FileWriter writer=new FileWriter("result.text")){
                writer.write(inputField.getText());
                JOptionPane.showMessageDialog(this,"保存成功");
            }catch(IOException ex){
                JOptionPane.showMessageDialog(this,"保存失败：" + ex.getMessage());
            }
        }
    }

    public static void main(String[] args) {
        new Main();
    }
}



import java.sql.*;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws Exception {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");

        // TODO: 使用 PreparedStatement 插入学生信息
        Scanner sc = new Scanner(System.in);
        System.out.print("输入姓名: ");
        String name = sc.nextLine();
        System.out.print("输入年龄: ");
        int age = sc.nextInt();

        // _____
        String insertSql1="INSERT INTO student(name,age) VALUES (?,?)";
        PreparedStatement ps=conn.prepareStatement(insertSql1);
        ps.setString(1,name);
        ps.setInt(2,age);
        ps.executeUpdate();
        System.out.println("插入成功！");


        // TODO: 使用 Statement 查询所有学生
        Statement stmt = conn.createStatement();
        String sql = "SELECT * FROM student";
        // _____
        ResultSet rs=stmt.executeQuery(sql);
        System.out.println("当前学生信息：");
        while(rs.next()){
            System.out.println(rs.getString("name")+" - "+rs.getInt("age"));
        }
        rs.close();
        stmt.close();
        ps.close();
        conn.close();
    }
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class Main extends JFrame implements ActionListener {
    private JTextField displayField;
    private JButton readButton;

    public Main() {
        displayField = new JTextField(20);
        displayField.setEditable(false); // 不可编辑
        add(displayField, BorderLayout.NORTH);

        readButton = new JButton("读取文件");
        // TODO: 添加监听器
        // ______
        readButton.addActionListener(this);

        add(readButton, BorderLayout.CENTER);

        setSize(400, 150);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO: 实现读取 data.txt 的内容，并显示
        try (BufferedReader reader=new BufferedReader(new FileReader("D:\\AAA\\算法分析.docx"))){
            String line=reader.readLine();
            displayField.setText(line);

            // ______
        } catch (IOException ex) {
            displayField.setText("读取失败：" + ex.getMessage());
        }
    }

    public static void main(String[] args) {
        new Main();
    }
}
import java.sql.*;
import java.util.Scanner;

public class Main  {
    public static void main(String[] args) throws Exception {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection conn = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/test", "root", "123456"
        );

        Scanner sc = new Scanner(System.in);
        System.out.print("输入要查询并删除的学生姓名：");
        String name = sc.nextLine();

        // TODO: 查询是否存在
        String querySql = "SELECT * FROM student WHERE name = ?";
        PreparedStatement psQuery = conn.prepareStatement(querySql);
        // _____
        psQuery.setString(1,name);
        ResultSet rs = psQuery.executeQuery();

        if (rs.next()) {
            System.out.println("学生已找到：" + rs.getString("name"));
            System.out.print("确认删除吗？(y/n)：");
            String confirm = sc.nextLine();
            if ("y".equalsIgnoreCase(confirm)) {
                // TODO: 执行删除操作
                String delSql = "DELETE FROM student WHERE name = ?";
                // ______
                PreparedStatement psDel=conn.prepareStatement(delSql);
                psDel.setString(1,name);
                psDel.executeUpdate();
                System.out.println("删除成功！");
                psDel.close();
            }
        } else {
            System.out.println("未找到该学生。");
        }

        rs.close();
        conn.close();
    }
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class Main extends JFrame implements ActionListener {
    private JTextField inputField;
    private JButton saveButton, appendButton;

    public Main() {
        inputField = new JTextField(20);
        add(inputField, BorderLayout.NORTH);

        JPanel panel = new JPanel();
        saveButton = new JButton("保存");
        appendButton = new JButton("追加");

        // TODO: 添加事件监听
        // ______
        saveButton.addActionListener(this);
        appendButton.addActionListener(this);

        panel.add(saveButton);
        panel.add(appendButton);
        add(panel, BorderLayout.CENTER);

        setSize(400, 150);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String text = inputField.getText();
        String cmd = e.getActionCommand();
        boolean append = "追加".equals(cmd);
        // TODO: 完成保存与追加功能
        // ______
        try(FileWriter writer=new FileWriter("log.txt",append)){
            writer.write(text + "\n");
            JOptionPane.showMessageDialog(this,append ?"追加成功" : "保存成功");
            inputField.setText("");
        }catch(IOException ex){
            JOptionPane.showMessageDialog(this, "写入失败：" + ex.getMessage());
        }
    }

    public static void main(String[] args) {
        new Main();
    }
}
import java.sql.*;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws Exception {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection conn = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/test", "root", "123456"
        );

        Scanner sc = new Scanner(System.in);
        System.out.print("输入要修改的学生ID：");
        int id = sc.nextInt();
        System.out.print("输入新的年龄：");
        int age = sc.nextInt();

        // TODO: 使用 PreparedStatement 完成更新操作
        String sql = "UPDATE student SET age = ? WHERE id = ?";
        // _______
        PreparedStatement ps=conn.prepareStatement(sql);
        ps.setInt(1,age);
        ps.setInt(2,id);
        ps.executeUpdate();

        conn.close();
    }
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Main extends JFrame implements ActionListener {
    private JTextField inputField;
    private JButton showButton;

    public Main() {
        inputField = new JTextField(20);
        showButton = new JButton("显示");

        // TODO①：添加按钮监听器
        // _______________________
        showButton.addActionListener(this);

        setLayout(new FlowLayout());
        add(inputField);
        add(showButton);

        setSize(300, 120);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO②：弹出对话框显示输入内容
        // _______________________
        String text=inputField.getText();
        JOptionPane.showMessageDialog(this,"你输入的是：" + text);
    }

    public static void main(String[] args) {
        new Main();
    }
}
import java.sql.*;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws Exception {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection conn = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/test", "root", "123456"
        );

        Scanner sc = new Scanner(System.in);
        System.out.print("输入姓名：");
        String name = sc.nextLine();
        System.out.print("输入年龄：");
        int age = sc.nextInt();

        // TODO：使用 PreparedStatement 插入数据
        String sql = "INSERT INTO student(name, age) VALUES (?, ?)";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1,name);
        ps.setInt(2,age);
        // _______________________
        // _______________________

        ps.executeUpdate();
        System.out.println("插入成功！");
        conn.close();
    }
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Main extends JFrame implements ActionListener {
    private JTextField textField;
    private JButton clearButton;

    public Main() {
        setTitle("清除文本框内容");
        setLayout(new FlowLayout());
        setSize(300, 100);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        textField = new JTextField(20);
        clearButton = new JButton("清除");

        // TODO①：添加按钮监听器
        // _______________________
        clearButton.addActionListener(this);
        add(textField);
        add(clearButton);
        setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO②：将文本框内容清空
        // _______________________
        textField.setText("");
    }

    public static void main(String[] args) {
        new Main();
    }
}
import java.sql.*;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws Exception {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection conn = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/test", "root", "123456"
        );

        Scanner sc = new Scanner(System.in);
        System.out.print("输入要删除的学生姓名：");
        String name = sc.nextLine();

        // TODO：使用 PreparedStatement 删除记录
        String sql = "DELETE FROM student WHERE name = ?";
        PreparedStatement ps = conn.prepareStatement(sql);
        // _______________________
        ps.setString(1,name);
        int rows = ps.executeUpdate();
        if (rows > 0) {
            System.out.println("删除成功！");
        } else {
            System.out.println("未找到该学生。");
        }

        conn.close();
    }
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Main extends JFrame implements ActionListener {
    private JTextField textField;
    private JButton helloButton, clearButton;

    public Main() {
        setTitle("两个按钮示例");
        setLayout(new FlowLayout());
        setSize(350, 120);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        textField = new JTextField(20);
        helloButton = new JButton("设置为Hello");
        clearButton = new JButton("清空");

        // TODO①：添加按钮监听器
        // _______________________
        helloButton.addActionListener(this);
        clearButton.addActionListener(this);

        add(textField);
        add(helloButton);
        add(clearButton);
        setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO②：判断事件来源并设置文本框内容
        // _______________________
        if(e.getSource()==helloButton){
            textField.setText("Hello");
        }else if(e.getSource()==clearButton){
            textField.setText("");
        }
    }

    public static void main(String[] args) {
        new Main();
    }
}
import java.sql.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection conn = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/test", "root", "123456"
        );

        Statement stmt = conn.createStatement();
        String sql = "SELECT * FROM student";

        // TODO：执行查询并遍历结果输出
        // _______________________
        ResultSet rs=stmt.executeQuery(sql);
        while(rs.next()){
            int id=rs.getInt("id");
            String name=rs.getString("name");
            int age=rs.getInt("age");
            System.out.println("ID: " + id + ", 姓名: " + name + ", 年龄: " + age);
        }

        conn.close();
    }
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class Main extends JFrame implements ActionListener {
    private JTextField fileField;
    private JTextField contentField;
    private JButton saveButton;

    public Main() {
        fileField = new JTextField(15);
        contentField = new JTextField(15);
        saveButton = new JButton("保存");

        // TODO①：添加监听器
        // __________________________
        saveButton.addActionListener(this);

        setLayout(new GridLayout(3, 2));
        add(new JLabel("文件名："));
        add(fileField);
        add(new JLabel("内容："));
        add(contentField);
        add(new JLabel());
        add(saveButton);

        setSize(350, 150);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO②：获取文件名和内容并写入
        // __________________________
        String fileName=fileField.getText().trim();
        String content=contentField.getText();
        try(FileWriter writer=new FileWriter(fileName)){
            writer.write(content);
            JOptionPane.showMessageDialog(this,"保存成功！");
        }catch(IOException ex){
            JOptionPane.showMessageDialog(this, "保存失败：" + ex.getMessage());
        }
    }

    public static void main(String[] args) {
        new Main();
    }
}
import java.sql.*;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws Exception {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection conn = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/test", "root", "123456"
        );

        Scanner sc = new Scanner(System.in);
        System.out.print("输入姓名关键字：");
        String keyword = sc.nextLine();

        // TODO：使用 PreparedStatement 实现模糊查询
        String sql = "SELECT name, age FROM student WHERE name LIKE ?";
        PreparedStatement ps = conn.prepareStatement(sql);
        // __________________________
        ps.setString(1,"%" + keyword + "%");

        ResultSet rs = ps.executeQuery();
        while (rs.next()) {
            String name = rs.getString("name");
            int age = rs.getInt("age");
            System.out.println("姓名：" + name + "，年龄：" + age);
        }

        rs.close();
        conn.close();
    }
}
